package org.fly.controller.wap.customer;

import org.fly.bean.Cart;
import org.fly.bean.Customer;
import org.fly.bean.Page;
import org.fly.bean.query.CartQuery;
import org.fly.controller.AbstractController;
import org.fly.service.CartService;
import org.fly.utils.JsonUtils;
import org.fly.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.soap.Addressing;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/4/9.
 */
@Controller
@RequestMapping("/wap/customer/cart/")
public class WapCCartController extends AbstractController {

    @Autowired
    CartService cartService;

    @RequestMapping(value = "add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(Cart cart, HttpServletRequest request){
        try {
            Customer customer = (Customer) UserContext.getUser();
            String customerId = customer.getId();
            cart.setCustomerId(customerId);
            cartService.save(cart);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "updateCart", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateCart(Cart cart, HttpServletRequest request){
        try {
            Customer customer = (Customer) UserContext.getUser();
            String customerId = customer.getId();
            cart.setCustomerId(customerId);
            cartService.save(cart);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "queryPage", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryPage(CartQuery query){
        Customer customer = (Customer) UserContext.getUser();
        String customerId = customer.getId();
        query.setCustomerId(customerId);
        Page page = cartService.queryGroupPage(query);
        return JsonUtils.encode(createDataTip(page));
    }
}
